"""
Algorithms (optional):

You are given a binary random number generator rand2() that generates binary numbers 0 and 1 with equal probability.
Use rand2() as a random source to write a new function rand() that generates 0 with 10% probability and 1 with 90%
probability.

"""
import random


def rand2():
    return random.randint(0, 1)


def rand2_19():
    """
    ['0000', '0001', '0010', '0011', '0100', '0101', '0110', '0111',
    '1000', '1001', '1010', '1011', '1100', '1101', '1110', '1111']
    利用二进制调用四次rand2产生 四位二进制的数在10进制中[0,15]
    若等于0就返回0若大于0小于10就返回1
    若是产生的数在[10，15]就递归调用直到得到返回值
    :return:      num0:9950,num1:90050
    """
    num = 0
    for i in range(4):
        num |= rand2() << i
    if num == 0:
        return 0
    elif 10 > num > 0:
        return 1
    else:
        return rand2_19()


arr = [rand2_19() for _ in range(100000)]
print("num0:{},num1:{}".format(arr.count(0), arr.count(1)))
"num0:9950,num1:90050"
